package com.science.slam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.science.slam.domain.LikeTable;
import com.science.slam.service.FileService;
import com.science.slam.service.LikeTableService;
import com.science.slam.mapper.LikeTableMapper;
import com.science.slam.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LikeTableServiceImpl extends ServiceImpl<LikeTableMapper, LikeTable>
    implements LikeTableService{
    @Autowired
    LikeTableMapper likeMapper;
    @Autowired
    FileService fileService;

    @Override
    public Integer UnLike(String userID, String fileID) {
        QueryWrapper<LikeTable> likeTableQueryWrapper = new QueryWrapper<>();
        likeTableQueryWrapper.eq("user_id", userID);
        likeTableQueryWrapper.eq("file_id", fileID);
        int row = likeMapper.delete(likeTableQueryWrapper);
        if (row == 0)
            return row;
        Integer i = fileService.decrementLikeNumber(fileID);
        return i;
    }

    @Override
    public Integer Like(String userID, String fileID) {
        String id = IDUtils.uniqueLikeID();
        LikeTable likeTable = new LikeTable(id, userID, fileID);
        int row = likeMapper.insert(likeTable);
        Integer i = fileService.incrementLikeNumber(fileID);
        if (row == 1 && i == 1)
            return 1;
        return 0;
    }
}




